//
//  LocationManager.m
//  ScholarsBridge
//
//  Created by 李亚琪 on 2017/1/4.
//  Copyright © 2017年 mingthink. All rights reserved.
//

#import "LocationManager.h"


@implementation LocationManager

//定位回调代理
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [manager stopUpdatingLocation];
    for(CLLocation *location in locations){
        DeLog(@"---------%@-------",location);
    }
    CLLocation *currLocation=[locations lastObject];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];//反向解析，根据及纬度反向解析出地址
    CLLocation *location = [locations objectAtIndex:0];
    [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        
        NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
        for(CLPlacemark *place in placemarks)
        {
            //取出当前位置的坐标
            DeLog(@"latitude : %f,longitude: %f",currLocation.coordinate.latitude,currLocation.coordinate.longitude);
            NSString *latStr = [NSString stringWithFormat:@"%f",currLocation.coordinate.latitude];
            NSString *lngStr = [NSString stringWithFormat:@"%f",currLocation.coordinate.longitude];
            NSDictionary *dict = [place addressDictionary];
            
            [resultDic setObject:dict[@"SubLocality"] forKey:@"xian"];
            [resultDic setObject:dict[@"City"] forKey:@"shi"];
            [resultDic setObject:latStr forKey:@"wei"];
            [resultDic setObject:lngStr forKey:@"jing"];
            [resultDic setObject:dict[@"State"] forKey:@"sheng"];
            [resultDic setObject:dict[@"Name"] forKey:@"all"];
        }
        DeLog(@"------addressDictionary-%@------",resultDic);
        if (self.successBlock) self.successBlock(resultDic);
    }];
}
#pragma mark - 检测应用是否开启定位服务
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    [manager stopUpdatingLocation];
    switch([error code]) {
        case kCLErrorDenied:
            [self openGPSTips];
            break;
        case kCLErrorLocationUnknown:
            break;
        default:
            break;
    }
}

-(void)openGPSTips{
    UIAlertView *alet = [[UIAlertView alloc] initWithTitle:@"当前定位服务不可用" message:@"请到“设置->隐私->定位服务”中开启定位" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alet show];
}
//获取定位信息
-(void)startUpdatingLocation{
    //开始定位
    [self.locaManager startUpdatingLocation];
}

-(CLLocationManager *)locaManager{
    if (!_locaManager){
        //初始化定位管理类
        _locaManager = [[CLLocationManager alloc] init];
        //delegate
        _locaManager.delegate = self;
        //The desired location accuracy.//精确度
        _locaManager.desiredAccuracy = kCLLocationAccuracyBest;
        //距离
        _locaManager.distanceFilter = 10;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {
            [_locaManager requestWhenInUseAuthorization];
            [_locaManager requestAlwaysAuthorization];
        }
    }
    return _locaManager;
}

-(NSArray *)getHomeCityList{
    if (arr_citys.count > 0){
        return arr_citys;
    }else{
        return nil;
    }
}

#pragma mark -- 加载城市
-(void)loadCitys{
    NSMutableDictionary *dic_params = NSMutableDictionary.new;
//    [HttpManager requestWithProgressTitle:nil method:HTTPS_POST path:URL_GET_AREAS paramDict:dic_params finishBlock:^(id result, AFHTTPRequestOperationManager *manager) {
//        if (![MyUtility isEmpty:result[@"Data"]]) {
//            arr_citys = result[@"Data"];
//        }
//    }];
}

+ (LocationManager *)sharedManager{
    static LocationManager *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
        [sharedAccountManagerInstance loadCitys];
    });
    return sharedAccountManagerInstance;
}

@end
